package com.itholmes.utils;

import java.util.Date;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
//池化技术：BasePooledObjectFactory
public class JedisClusterPoolFactory extends BasePooledObjectFactory<JedisCluster> {

	private HostAndPort hostAndPort = new HostAndPort("39.103.163.156", 6379);
	
	public JedisClusterPoolFactory() {
	}

	public JedisClusterPoolFactory(HostAndPort hostAndPort) {
		this.hostAndPort = hostAndPort;
	}

	//对象创建
	@Override
	public JedisCluster create() throws Exception {
		System.out.println("创建："+new Date());
		JedisCluster cluster = new JedisCluster(hostAndPort);
		return cluster;
	}

	//wrap包装
	@Override
	public PooledObject<JedisCluster> wrap(JedisCluster obj) {
		System.out.println("包装：" + new Date());
		PooledObject<JedisCluster> pooledObject = new DefaultPooledObject<JedisCluster>(obj);
		return pooledObject;
	}

	//activate激活
	@Override
	public void activateObject(PooledObject<JedisCluster> pooledObject) throws Exception {
		System.out.println("激活：" + new Date());
		super.activateObject(pooledObject);
	}

	//对象销毁
	@Override
	public void destroyObject(PooledObject<JedisCluster> pooledObject) throws Exception {
		System.out.println("销毁：" + new Date());
		JedisCluster cluster = pooledObject.getObject();
		cluster.close();
		super.destroyObject(pooledObject);
	}

}
